package appcus

import (
	"counter-help/admin/server/global"
	"counter-help/admin/server/model/common/response"
	"counter-help/admin/server/utils"
	"github.com/gin-gonic/gin"
	"go.uber.org/zap"
	"strconv"
)

type CusOrderFavoriteApi struct{}

// FavoriteCusOrder 收藏订单
// @Tags CusOrder
// @Summary 收藏订单
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param orderID query string true "订单ID"
// @Success 200 {object} response.Response{msg=string} "收藏成功"
// @Router /cusOrder/favoriteCusOrder [post]
func (cusOrderFavoriteApi *CusOrderFavoriteApi) FavoriteCusOrder(c *gin.Context) {
	// 定义请求体结构体
	type FavoriteRequest struct {
		OrderID int `json:"orderID" binding:"required"` // 直接接受整数类型的 orderID
	}

	// 绑定 JSON 参数
	var req FavoriteRequest
	if err := c.ShouldBindJSON(&req); err != nil {
		response.FailWithMessage("参数无效", c)
		return
	}

	// 执行收藏操作
	userID := utils.GetUserID(c)
	err := cusOrderFavoriteService.FavoriteOrderByID(int(userID), req.OrderID)
	if err != nil {
		global.GVA_LOG.Error("收藏失败!", zap.Error(err))
		response.FailWithMessage("收藏失败:"+err.Error(), c)
		return
	}

	response.OkWithMessage("收藏成功", c)
}

// GetFavoriteOrders 获取收藏的订单列表
// @Tags CusOrder
// @Summary 获取收藏的订单列表
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Success 200 {object} response.Response{data=[]customer.CusOrder,msg=string} "获取成功"
// @Router /cusOrder/getFavoriteOrders [get]
func (cusOrderFavoriteApi *CusOrderFavoriteApi) GetFavoriteOrders(c *gin.Context) {
	// 获取当前用户的收藏订单列表
	userID := utils.GetUserID(c)
	favoriteOrders, err := cusOrderFavoriteService.GetFavoriteOrdersByUserID(int(userID))
	if err != nil {
		global.GVA_LOG.Error("获取收藏列表失败!", zap.Error(err))
		response.FailWithMessage("获取收藏列表失败:"+err.Error(), c)
		return
	}

	response.OkWithDetailed(favoriteOrders, "获取成功", c)
}

// UnfavoriteCusOrder 取消收藏订单
// @Tags CusOrder
// @Summary 取消收藏订单
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param orderID query string true "订单ID"
// @Success 200 {object} response.Response{msg=string} "取消收藏成功"
// @Router /cusOrder/unfavoriteCusOrder [delete]
func (cusOrderFavoriteApi *CusOrderFavoriteApi) UnfavoriteCusOrder(c *gin.Context) {
	// 获取订单ID并转换为整数
	orderIDStr := c.Param("orderID")
	orderID, err := strconv.Atoi(orderIDStr)
	if err != nil {
		response.FailWithMessage("订单ID无效", c)
		return
	}

	// 执行取消收藏操作
	userID := utils.GetUserID(c)
	err = cusOrderFavoriteService.UnfavoriteOrderByID(int(userID), orderID)
	if err != nil {
		global.GVA_LOG.Error("取消收藏失败!", zap.Error(err))
		response.FailWithMessage("取消收藏失败:"+err.Error(), c)
		return
	}

	response.OkWithMessage("取消收藏成功", c)
}
